package com.kuang.jdbc;

import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class jdbcTran1 {
    @Test
    public void test(){
        String url="jdbc:mysql://localhost:8806/jdbc?useSSL=false&UseUnicode=true&characterEncoding=utf-8";
        String username="root";
        String password="123456";
        Connection connection=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");

            connection = DriverManager.getConnection(url, username, password);

            connection.setAutoCommit(false);

            String sql="update account set money=money-100 where name='A'";
            connection.prepareStatement(sql).executeUpdate();

//            int i=1/0;  //java.lang.ArithmeticException: / by zero

            String sql2="update account set money=money+100 where name='B'";
            connection.prepareStatement(sql2).executeUpdate();

            connection.commit();
            System.out.println("提交成功");


        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
